package com.dgut.courses_selecting_sys.controller;


import com.dgut.courses_selecting_sys.constants.ActivityStatus;
import com.dgut.courses_selecting_sys.constants.CourseStatus;
import com.dgut.courses_selecting_sys.dto.ActivitiesDto;
import com.dgut.courses_selecting_sys.entity.*;
import com.dgut.courses_selecting_sys.mapper.UserActivitiesMapper;
import com.dgut.courses_selecting_sys.service.IActivitiesService;
import com.dgut.courses_selecting_sys.service.IUserActivitiesService;
import com.dgut.courses_selecting_sys.service.IUserRoleService;
import com.dgut.courses_selecting_sys.utils.RedisUtil;
import com.dgut.courses_selecting_sys.utils.ShiroUtils;
import com.dgut.courses_selecting_sys.utils.core.BeanUtils;
import com.dgut.courses_selecting_sys.vo.ActivityVo;
import com.dgut.courses_selecting_sys.vo.PageVo;
import com.dgut.courses_selecting_sys.vo.UserCourseListVo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yuec
 * @since 2021-01-26
 */
@Controller
@RequestMapping("//activities")
public class ActivitiesController {

    @Autowired
    IActivitiesService activitiesService;
    @Autowired
    IUserRoleService userRoleService;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    IUserActivitiesService userActivitiesService;

    /**
     * 返回创建活动的HTML的页面
     *
     * @return
     */
    @GetMapping("/create")
    public String create() {
        return "activities/create";
    }


    /**
     * 修改活动html
     *
     * @param model
     * @param activityId
     * @return
     */
    @RequiresRoles(value = {"teacher", "admin"}, logical = Logical.OR)
    @GetMapping("/edit")
    public String edit(Model model, @RequestParam("activityId") Long activityId) {
        Activities activities = activitiesService.getById(activityId);
        ActivitiesDto activitiesDto = new ActivitiesDto();
        BeanUtils.copyBeanProp(activitiesDto, activities);
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
        String start = ft.format(activities.getStartTime());
        String end = ft.format(activities.getEndTime());
        activitiesDto.setStartTime(start);
        activitiesDto.setEndTime(end);
        model.addAttribute("activities", activitiesDto);
        if (!activities.getCreateBy().equals(ShiroUtils.getSysUser().getUserId())) {
            model.addAttribute("Authority", "-(无权限修改)");
        }
        return "activities/edit";
    }

    @GetMapping("/choose")
    public String choose(ModelMap modelMap) {
        User user = ShiroUtils.getSysUser();
        //更新课程状态
        List<Activities> activities = activitiesService.listActivities();
        List<ActivitiesDto> activitiesDtos=activities.stream().map(
                a->{
                    ActivitiesDto activitiesDto = new ActivitiesDto();
                    BeanUtils.copyBeanProp(activitiesDto,a);
                    SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    String start = ft.format(a.getStartTime());
                    String end = ft.format(a.getEndTime());
                    activitiesDto.setStartTime(start);
                    activitiesDto.setEndTime(end);
                    return activitiesDto;
                }
        ).collect(Collectors.toList());
        modelMap.addAttribute("activitiesList", activitiesDtos);
        return "activities/choose";
    }

    @ResponseBody
    @GetMapping("/checkUserChoose")
    public AjaxResult checkUserChoose() {
        User user = ShiroUtils.getSysUser();
        //检查用户选择状态
        return AjaxResult.success(activitiesService.checkUserActivitiesForChoose(user));
    }


    @GetMapping("/history")
    public String activitiesHistory() {
        return "activities/history";
    }

    /**
     * 获取历史/按条件获取结果集
     *
     * @param activityVo
     * @return
     */
    @ResponseBody
    @RequestMapping("/history/getData")
    public PageVo<Activities> getData(ActivityVo activityVo) {
        return activitiesService.getDataByActivityVo(activityVo);
    }


    /**
     * 創建活动
     *
     * @param activities
     * @return
     */
    @ResponseBody
    @PostMapping("/createActivity")
    public AjaxResult createActivity(@Validated ActivitiesDto activities) {
        return activitiesService.createActivity(activities);
    }


    @RequiresRoles(value = {"teacher", "admin"}, logical = Logical.OR)
    @ResponseBody
    @GetMapping("/editActivity")
    public AjaxResult editActivity(Activities activities) {
        return activitiesService.editActivity(activities);
    }

    /**
     * 报选活动
     * get请求考虑其幂等性
     *
     * @param activityId
     * @return
     */
    @ResponseBody
    @GetMapping("/chooseActivity")
    public AjaxResult chooseActivity(long activityId) {
        return activitiesService.chooseActivity(ShiroUtils.getSysUser().getUserId(), activityId);
    }


    /**
     * 取消选择
     * 退选
     *
     * @param activityId
     * @return
     */
    @ResponseBody
    @PostMapping("/unChoose")
    public AjaxResult unChooseActivity(long activityId) {
        return activitiesService.unChooseActivity(ShiroUtils.getSysUser().getUserId(), activityId);
    }

    /**
     * 返回课程选报情况
     *
     * @return
     */
    @RequiresRoles(value = {"teacher", "admin"}, logical = Logical.OR)
    @GetMapping("/chooseList")
    public String chooseList(Model model, @RequestParam("activityId") int activityId) {
        List<UserCourseListVo> userCourseList = userActivitiesService.listByActivityId(activityId);
        model.addAttribute("userCourseList", userCourseList);
        Activities activities = activitiesService.getById(activityId);
        model.addAttribute("courseName", activities.getActivityName());
        model.addAttribute("count",activities.getCount());
        model.addAttribute("chooseCount",userCourseList.size());
        return "course/chooseList";
    }

}
